package com.cy4;
/*
用于测试一个字符串中所有字符的个数
 */
import java.util.*;

public class HashMapDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要检测的字符串：");
        String s = sc.nextLine();
        TreeMap<Character, Integer> hm = new TreeMap<>();//和TreeSet类似，对存入的元素进行排序，无参构造默认为自然排序
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (hm.get(c) == null) {
                hm.put(c, 1);
            } else {
                Integer integer = hm.get(c);
                integer++;
                hm.put(c, integer);
            }
        }
        Set<Character> characters = hm.keySet();
        StringBuilder sb = new StringBuilder();
        for (Character character : characters) {
            Integer integer = hm.get(character);
            sb.append(character).append("(").append(integer).append(")");
        }
        String string = sb.toString();
        System.out.println(string);
    }
}
